package com.cn.yunhe.rentcars.web;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.cn.yunhe.rentcars.mapper.CarMapper;
import com.cn.yunhe.rentcars.mapper.SelfUseMapper;
import com.cn.yunhe.rentcars.mapper.UserMapper;
import com.cn.yunhe.rentcars.model.Car;
import com.cn.yunhe.rentcars.model.SelfUse;
import com.cn.yunhe.rentcars.model.User;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@RestController
public class SelfUseController {
	
	//增加	URL：selfuser/add method:post
	//删除	URL：selfuser/delete method:delete
	//查询	URL：selfuser/select method:get
	//修改	URL：selfuser/query method:put
	@Autowired
	private SelfUseMapper selfUseMapper;
	@Autowired
	private CarMapper carMapper ;

	@RequestMapping(value="/selfuse-list", method=RequestMethod.GET)
	public ModelAndView selfUseList() {	
		return new ModelAndView("selfuse-list");
	}

	@RequestMapping(value="/selfuse-add", method=RequestMethod.GET)
	public ModelAndView showAddSelfUse() {	
		return new ModelAndView("selfuse-add");
	}
	
	@RequestMapping(value="/selfuse-edit", method=RequestMethod.GET)
	public ModelAndView showEditSelfUse(@RequestParam String recordid) {	
		
		SelfUse selfUse = selfUseMapper.getSelfUseById(recordid);
		
		Map<String,SelfUse> modelMap = new HashMap<String,SelfUse>();
		modelMap.put("selfUse", selfUse);
		
		return new ModelAndView("selfuse-edit",modelMap);		
	}
	
	@RequestMapping(value="/getselfusebycarid", method=RequestMethod.GET)
	public PageInfo<SelfUse> getSelfusebycarid(@RequestParam String carid,@RequestParam  int pageNum,@RequestParam  int pageSize){
	      PageHelper.startPage(pageNum, pageSize);
	      List<SelfUse> selfUseList = selfUseMapper.getSelfUse(carid);
	      PageInfo<SelfUse> page = new PageInfo<SelfUse>(selfUseList);
	      return page;
	}
	
	
	/**
	 * 添加新用户
	 * 
	 * @param userid
	 * @param isabled
	 */
	@RequestMapping(value="/addSelfUse", method=RequestMethod.POST)
	public void addSelfUse(@RequestBody SelfUse selfUse){		
		selfUse.setRecordid(UUID.randomUUID().toString());
		selfUseMapper.addSelfUse(selfUse);
		
		Car car = new Car ();
		if(selfUse.getReturntime() !=""){
			car.setCarstate(6);
		}else{
			car.setCarstate(8);
		}		
		car.setCarcode(selfUse.getCarid());			
		carMapper.setCarAllstate(car);		
		
	}
	
	/**
	 * 修改用户
	 * 
	 * @param userid
	 */
	@RequestMapping(value="/selfUse/{recordid}", method=RequestMethod.PUT)
	public void editaddSelfUse(@PathVariable("recordid") String recordid,@RequestBody SelfUse selfUse){
		selfUse.setRecordid(recordid);
		selfUseMapper.editSelfUse(selfUse);

		if(selfUse.getReturntime() !=""){
			Car car = new Car ();
			car.setCarstate(6);
			car.setCarcode(selfUse.getCarid());			
			carMapper.setCarAllstate(car);
		}	
		
	}
	

	
	/* * 编辑用户状态
	 * 
	 * @param userid
	 * @param isabled
	 
	@RequestMapping(value="/setUserState", method=RequestMethod.POST)
	public void handleUserState(@RequestParam String recordid,@RequestParam  int isabled){ 
		SelfUse selfUse = new SelfUse();
		selfUse.setRecordid(recordid);
		selfUse.setIsabled(isabled);
		selfUseMapper.setUserState(user);
	}*/

	/**
	 * 删除用户信息
	 * 
	 * @param userid
	 * @param isabled
	 */
	@RequestMapping(value="/delSelfUse", method=RequestMethod.POST)
	public void delSelfUse(@RequestParam String recordid){ 
		selfUseMapper.delSelfUse(recordid);
	}
	
	/**
	 * 删除用户信息
	 * 
	 * @param userid
	 * @param isabled
	 */
	@RequestMapping(value="/delSelfUses", method=RequestMethod.POST)
	public void delSelfUses(@RequestParam String recordid){ 
		List<String> recordidList = Arrays.asList(recordid.split(","));  		
		selfUseMapper.delSelfUses(recordidList);
	}
	
	
	/**
	 * 搜索查询用户列表信息
	 * 
	 * @param username
	 * @param draw
	 * @param start
	 * @param length
	 * @return
	 */
	@RequestMapping(value="/selfUse", method=RequestMethod.GET)
	public Map<String,Object> getLikeSelfUse(@RequestParam String datemin,@RequestParam String datemax,@RequestParam String carid,@RequestParam int draw,@RequestParam int start,@RequestParam int length){
		  
		  Map<String,String> params =  new HashMap<String,String>();
		  params.put("carid", carid);
		  params.put("datemin", datemin);
		  params.put("datemax", datemax);
		
		  int pageNum = (start/length)+1;
		  int pageSize = length;		 
		  
	      PageHelper.startPage(pageNum, pageSize);
	      List<SelfUse> selfUseList = selfUseMapper.searchSelfUse(params);
	      PageInfo<SelfUse> page = new PageInfo<SelfUse>(selfUseList);
	      
	      Map<String,Object> rsMap = new HashMap<String,Object>();
	      rsMap.put("draw",draw);
	      rsMap.put("data", page.getList());	      
	      rsMap.put("recordsTotal", page.getTotal());
	      rsMap.put("recordsFiltered", page.getTotal());
	      return rsMap;
	}
}
